package com.mobinters.web;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


@SuppressLint("SetJavaScriptEnabled")
public class AsyncWebView {
	WebView mWeb;
	protected static final int LOADING_DIALOG = 0;
	Context mContext;
	private Dialog mDialog;
	private ProgressBar mProBar;
	
	public AsyncWebView(Context a,WebView wv,ProgressBar pro){
		
		mWeb=wv;
		mContext=a;
		mDialog=onCreateDialog(LOADING_DIALOG);
		mProBar=pro;
	}
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public void settings(int mode){
		switch(mode){
		case 0:			
			mWeb.setWebViewClient(new myWebClient());
			mWeb.getSettings().setJavaScriptEnabled(false);//maybe true
			mWeb.getSettings().setPluginsEnabled(true);
			mWeb.getSettings().setDisplayZoomControls(true);
		break;
		}
	}
	
	 public class myWebClient extends WebViewClient
	    {
	        @Override
	        public void onPageStarted(WebView view, String url, Bitmap favicon) {
	            // TODO Auto-generated method stub
	            super.onPageStarted(view, url, favicon);
	            
	            mProBar.setVisibility(View.VISIBLE);
	        }
	 
	        @Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            // TODO Auto-generated method stub
	        	//view.loadUrl("about:blank");
	            view.loadUrl(url);
	            return true;
	 
	        }
	 
	        @Override
	        public void onPageFinished(WebView view, String url) {
	            // TODO Auto-generated method stub
	            super.onPageFinished(view, url);
	 
	            mProBar.setVisibility(View.GONE);
	        }
	    }
	 
	public void loadUrl(String xURL){
		mWeb.loadUrl(xURL);
	}
	public void clearContent(){
		mWeb.loadUrl("about:blank");
	}
	
	public void loadAsync(String xURL){
		clearContent();
		new ListRefresher().execute(xURL);
	}
	
	protected Dialog onCreateDialog(int id) {
		if(id == LOADING_DIALOG){
			Log.e("show dialog","okie");
			ProgressDialog loadingDialog = new ProgressDialog(mContext);
			loadingDialog.setMessage("Loading video ...");
			loadingDialog.setIndeterminate(true);
			loadingDialog.setCancelable(true);
			return loadingDialog;
		} 
		return null;
	}	
	
	
	private void LoadData(String xURL){
			try {				
				mWeb.loadUrl(xURL);
			} catch (Exception e) {
				Log.e("XSKTOL", e.toString());
			}
		}
	 
	private class ListRefresher extends AsyncTask<String, Void, Void> {

		/**
		 * This is executed in the UI thread. The only place where we can show the dialog.
		 */
		@Override
		protected void onPreExecute() {
			mDialog.show();
		}
		
		/**
		 * This is executed in the background thread. 
		 */
		@Override
		protected Void doInBackground(String... params) {
			LoadData(params[0]);
			return null;
		}

		/**
		 * This is executed in the UI thread. The only place where we can show the dialog.
		 */
	   @Override 
       protected void onPostExecute(Void unused) { 
		   mDialog.dismiss();
       }
	}
}
